using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Web;
using CrystalDecisions.Shared;
using System.Data.SqlClient;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;

public partial class InvoicePage : BasePage
{

    private IInvoiceService invoiceService;
    private IGoodsService goodsService;

    public IGoodsService GoodsService
    {
        get { return goodsService; }
        set { goodsService = value; }
    }
    public IInvoiceService InvoiceService
    {
        get { return invoiceService; }
        set { invoiceService = value; }
    }
	
    public override string ActionName {
        get {
            return "vw_invoice";
        }
    }

    protected void Page_Load( object sender, EventArgs e ) {
        CheckCustomerType();

        if (!Page.IsPostBack)
            GetInvoices(0, true);
    }

    private void CheckCustomerType() {
        bool show = CommonHelper.ShowHiddenCustomers();
        gvInvoice.Columns[3].Visible = show;
        divHiddenCustomers.Visible = show;
    }

    private void GetInvoices(int pageNumber, bool setPageCount)
    {
        Invoice example = new Invoice();
        DateTime startDate = DateTime.Now.AddDays(-7);
        DateTime endDate = DateTime.Now;

        if (!String.IsNullOrEmpty(txtStartDate.Text))
            startDate = MyPersianDate.PersianToDateTime(txtStartDate.Text);

        if (!String.IsNullOrEmpty(txtEndDate.Text))
            endDate = MyPersianDate.PersianToDateTime(txtEndDate.Text);

        if (!divHiddenCustomers.Visible || rbFormalCustomers.Checked)
            example.IsFormal = true;
        else if (divHiddenCustomers.Visible && rbHiddenCustomers.Checked)
            example.IsFormal = false;

        if ( ddlGoods.SelectedValue != "-1" )
            example.Goods = goodsService.GetGoods(Convert.ToInt32(ddlGoods.SelectedValue));

        if (setPageCount)
            gvInvoice.VirtualItemCount = invoiceService.CountInvoices(example);

        gvInvoice.DataSource = invoiceService.FindInvoices(example, 15, pageNumber, startDate, endDate);
        gvInvoice.DataBind();

    }

    protected void CustomerTypeCheckedChanged( object sender, EventArgs e ) {
        gvInvoice.DataBind();
    }

    protected void btnSearch_Click( object sender, EventArgs e ) {
        GetInvoices(0, true);
    }

    protected void gvInvoice_SelectedIndexChanged( object sender, EventArgs e ) {
        int invoiceId = Convert.ToInt32( gvInvoice.DataKeys[gvInvoice.SelectedIndex].Value );
        Response.Redirect( "rpt/InvoiceReport.aspx?InvoiceId=" + invoiceId.ToString() );
    }

    protected void gvInvoice_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvInvoice.PageIndex = e.NewPageIndex;
        GetInvoices(e.NewPageIndex, false);
    }
}
